export class JsUtil {


    public static judgeLoadedByUrl(url: string){
        const js = /js$/i.test(url);
        return js ? this.loadScript(url) : this.loadLink(url);
    }

    public static loadLink(url:string) {
        return new Promise((resolve, reject) => {
            var link = document.createElement('link');
            link.rel = "stylesheet";
            link.type = "text/css";
            link.href = url;
            var head = document.getElementsByTagName("head")[0];
            head.appendChild(link);
            link.onload = resolve;
            link.onerror = reject;
        });
      
    }

    //加载js
    public static loadScript(url: string) {
        return new Promise((resolve, reject) => {
            const script = document.createElement("script");

            script.onload = resolve;
            script.onerror = reject;
            script.src = url;
            document.body.appendChild(script);
        });
    }
    //根据url判断是否加载了某个script，或者link
    public static isInclude(url: string) {
        var js = /js$/i.test(url);
        var es = document.getElementsByTagName(js ? 'script' : 'link');
        for (var i = 0; i < es.length; i++) {
            const e:any = es[i];
            if (e[js ? 'src' : 'href'].indexOf(url) != -1) {
                return true;
            }

        }
        return false;
    }
}